<?php

class lb_cron{
	
	/**
	 * constructor of lb_cron
	 * 
	 * @access	public
	 * @author	Armin Borumand
	 */
	function __construct(){
		$this->run();
	}
	
	/**
	 * Add a cron job
	 * 
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	cron name
	 * @param	string	file name
	 * @param	array		run times, keys= seconds, minutes, days, weeks
	 * @return	int		cron id
	 */
	function add($name, $file, $times){
		global $lb;
		$insert_in = '';
		$insert_val = '';
		
		$params[] = array($name, 'text');
		foreach($times as $key=>$val){
			$insert_in .= $key . ', ';
			$insert_val .= '#, ';
			$params[] = array($val ,'int');
		}
		$params[] = array($file, 'text');
		
		$query = 'INSERT INTO #__cron (name, ' . $insert_in . ' file) VALUES (#, ' . $insert_val . ' #)';
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] == 1){
			return $result['insert_id'];
		}else{
			return false;
		}
		
	}
	
	/**
	 * Edit a cron job
	 * 
	 * @access	public
	 * @author	Armin Borumand
	 * @param	string	cron name
	 * @param	string	file name
	 * @param	array		run times
	 * @return	boolean	true on success and false on failure
	 */
	function edit($id, $name, $file, $times){
		global $lb;
		$insert_in = '';
		$insert_val = '';
		
		$params[] = array($name, 'text');
		foreach($times as $key=>$val){
			$update_fld .= $key . ' = #, ';
			$params[] = array($val ,'int');
		}
		$params[] = array($file, 'text');
		$params[] = array($id, 'int');
		
		$query = 'UPDATE #__cron SET name = #' . $update_fld . 'file = # WHERE id=#';
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] == 1){
			return true;
		}else{
			return false;
		}
		
	}
	
	/**
	 *  Update the cron next run time
	 * 
	 * @access	public
	 * @author	Armin Borumand
	 * @param	int	cron id
	 * @param	int	next run time stamp from now
	 * @see 	run
	 */
	function update($id){
		global $lb;

		$params = array(
					array($id, 'int')
					);
		// @problem    update "next time" to "current time"+"timestamp"
		//					     or "previous time"+"timestamp"
		$query = 'UPDATE #__cron 
					SET time = TIMESTAMP(NOW() + seconds
									 + minutes*60 
									 + hours*60*60 
									 + days*24*60*60
									 + weeks*7*24*60*60)
					WHERE id = #';
		$lb['db']->query($query, $params);
	}
	
	/**
	 * Remove the cron job
	 * 
	 * @access	public
	 * @author	Armin Borumand
	 * @param	int	cron id
	 */
	function remove($id){
		global $lb;
		
		$params = array(
					array($id, 'int')
					);
		$query = 'DELETE FROM #__cron WHERE id = #';
		$lb['db']->query($query, $params);
	}
	
	/**
	 *  Run cron jobs
	 * 
	 * @access	public
	 * @author	Armin Borumand
	 * @return	boolean	true if run at least one job and false if no job to run
	 */
	function run(){
		global $lb;
	
		$query = 'SELECT * FROM #__cron WHERE time <= NOW()';
		$result = $lb['db']->query($query, array());
		//var_dump($result);
		if($result['num_of_rows'] > 0){
			foreach($result['rows'] as $rows){
				$this->run_file($rows['file']);
				$this->update($rows['id']);
			}
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * Run a file
	 * 
	 * @access	protected
	 * @author	Armin Borumand
	 * @param	string	filename
	 */
	function run_file($file){
	
		include($file);
	}
	
	/**
	* Return cron id
	*
	* @access	public
	* @author	Armin Borumand
	* @param	string	cron name
	* @return	int		cron id
	*/
	function get_id($name){
		global $lb;
		
		$params = array(
				array($name, 'text')
				);
		$query  = 'SELECT id FROM #__cron WHERE name = #';
		
		$result = $lb['db']->query($query, $params);
		
		if($result['num_of_rows'] == 1){
			return $result['rows'][0]['id'];
		}else{
			return false;
		}
	}

	/**
	* Return number of cron jobs
	*
	* @access	public
	* @author	Armin Borumand
	* @return	int	number of jobs
	*/
	function count(){
		global $lb;
		
		$result = $lb['db']->query('SELECT count(*) AS count FROM #__cron', array());
		
		if($result['num_of_rows'] > 0){
			return $result['rows'][0]['count'];
		}else{
			return -1;
		}
	}
}
?>